<template>
    <div class="app-container">
        <div style="margin-bottom: 10px;font-size: 10px;">
            选择：{{ baseMap.hosname }}  /  {{ depname }}  /  {{ workDate }}</div>
            <el-container style="height: 100%">
                <el-aside width="200px" style="border: 1px silver solid">
                    <!-- 部门 ，树形结构  data数据信息 defaultProps子节点情况-->
                    <el-tree
                    :data="data"
                    :props="defaultProps"
                    :default-expand-all="true"
                    @node-click="handleNodeClick">
                    </el-tree>
                </el-aside>
                <el-main style="padding: 0 0 0 20px;">
                    <el-row style="width: 100%">
                    <!-- 排班日期 分页 -->
                    <el-tag v-for="(item,index) in bookingScheduleList" :key="item.id" @click="selectDate(item.workDate, index)" :type="index == activeIndex ? '' : 'info'" style="height: 60px;margin-right: 5px;margin-right:15px;cursor:pointer;">
                        {{ item.workDate }} {{ item.dayOfWeek }}<br/>
                        {{ item.availableNumber }} / {{ item.reservedNumber }}
                    </el-tag>

                    <!-- 分页 -->
                    <el-pagination
                        :current-page="page"
                        :total="total"
                        :page-size="limit"
                        class="pagination"
                        layout="prev, pager, next"
                        @current-change="getPage">
                    </el-pagination>
                    </el-row>
                    <el-row style="margin-top: 20px;">
                    <!-- 排班日期对应的排班医生 -->
                        <el-table
                            v-loading="listLoading"
                            :data="scheduleList"
                            border
                            fit
                            highlight-current-row>
                            <el-table-column
                                        label="序号"
                                        width="140"
                                        align="center">
                                <template slot-scope="scope">
                                {{ scope.$index + 1 }}
                                </template>
                            </el-table-column>

                            <el-table-column label="职称" width="270" align="center">
                                <template slot-scope="scope">
                                {{ scope.row.title }} | {{ scope.row.docname }}
                                </template>
                            </el-table-column>

                            <el-table-column label="号源时间" width="180">
                                <template slot-scope="scope">
                                {{ scope.row.workTime == 0 ? "上午" : "下午" }}
                                </template>
                            </el-table-column>

                            <el-table-column prop="reservedNumber" label="可预约数" width="80"/>
                            <el-table-column prop="availableNumber" label="剩余预约数" width="100"/>
                            <el-table-column prop="amount" label="挂号费(元)" width="90"/>
                            <el-table-column prop="skill" label="擅长技能"/>
                        </el-table>
                    </el-row>
                </el-main>
            </el-container>
      </div>
</template>

<script>
import  hospitalApi  from '@/api/hosp'

export default{
    data(){
        return{
            data: [],//接口返回的数据
            defaultProps: {  //子节点的信息
                children: 'children', 
                label: 'depname' //字节点科室的名称
            },
            hoscode: null,//医院编号
            activeIndex: 0,//上面样式值
            depcode: null,//科室编号
            depname: null,//科室名称
            workDate: null,//工作日期

            bookingScheduleList: [],//科室列表
            baseMap: {},//医院信息，根据医院编号得知医院名称

            page: 1, // 当前页
            limit: 6, // 每页个数
            total: 0, // 总页码

            scheduleList:[]//排班详情 
        }
    },
    created(){
        this.hoscode= this.$route.params.hoscode
        this.workDate = this.getCurDate()
        this.fetchData()
    },
    methods:{
        //排班具体信息查询
        getScheduleDetil(){
            hospitalApi.scheduleDetailList(this.hoscode,this.depcode,this.workDate)
                .then(response=>{
                    this.scheduleList = response.data
                })
        },
        //查询医院科室列表
        fetchData(){
            hospitalApi.departmentList(this.hoscode)
                .then(response=>{
                    this.data=response.data
                    // 默认选中第一个
                    if (this.data.length > 0) {
                        this.depcode = this.data[0].children[0].depcode
                        this.depname = this.data[0].children[0].depname

                        this.getPage()
                    }
                })
        },

        getPage(page = 1) {
            this.page = page
            this.workDate = null
            this.activeIndex = 0
            this.getScheduleRule()
        },
        //拍排班规则详情（就是上方显示的日期之类的）
        getScheduleRule() {
            hospitalApi.scheduleList(this.page, this.limit, this.hoscode, this.depcode).then(response => {
                this.bookingScheduleList = response.data.bookingScheduleRuleList

                this.total = response.data.total

                this.scheduleList = response.data.scheduleList//排班具体详情
                this.baseMap = response.data.baseMap

                // 分页后workDate=null，默认选中第一个
                if (this.workDate == null) {
                
                    this.workDate = this.bookingScheduleList[0].workDate
                }
                //排班详情调用
                this.getScheduleDetil()
        })
    },
    //对应上方点击科室之后右边陈列显示的数据
    handleNodeClick(data) {
        // 科室大类直接返回
        if (data.children != null) return //节点不等于空，就返回，说明点击的就是大科室，大科室无法看到排班，只有详细科室才能看到
            this.depcode = data.depcode //如果为空，将科室编号和科室信息都赋值
            this.depname = data.depname

        this.getPage(1) //分页方法调用，显示的就是第一页
    },
    //预约日期选择
    selectDate(workDate, index) {
        this.workDate = workDate
        this.activeIndex = index
        //排班详情调用
        this.getScheduleDetil()
    },

    //得到当前日期
    getCurDate() {
        var datetime = new Date()
        var year = datetime.getFullYear()
        var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1
        var date = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate()
        return year + '-' + month + '-' + date
        }
    }
}
</script>
<style>
.el-tree-node.is-current > .el-tree-node__content {
    background-color: #409EFF!important;
    color: white;
    }
.el-checkbox__input.is-checked+.el-checkbox__label {
    color: black;
    }
</style>